<template>
  <el-dialog
    v-dialogDrag
    :visible="isDialogVisible"
    :title="titleName"
    center
    width="70%"
    :append-to-body="true"
    :close-on-click-modal="false"
    @close="closeEdit"
  >
    <dialogHeader slot="title" :title="titleName" :style-type="$store.state.app.dialogType" />
    <div class="filter-container filter-params">
      <one-table-template
        ref="multipleTable"
        :dynamic-api-config="dynamicApiConfig"
        :dynamic-buttons="dynamicButtons"
        :dynamic-components="tableComponents"
        :dynamic-form-fields="formField"
        :dynamic-is-show-more-btn="true"
        :dynamic-is-show-select="true"
      />
    </div>
  </el-dialog>
</template>

<script>
import dialogHeader from '@/components/dialogHeader'
import OneTableTemplate from '@/components/templates/oneTable'
import api from '@/api/apiVeData'
export default {
  components: {
    dialogHeader,
    OneTableTemplate
  },
  props: {
    isDialogVisible: {
      type: Boolean,
      default: false
    },
    dataForForm: {
      type: Object,
      default: () => {}
    },
    titleName: {
      type: String,
      default: ''
    },
    name: {
      type: String,
      default: ''

    },
    editAdd: {
      type: String,
      default: ''
    },
    editData: {
      type: Object,
      default: () => {}
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      editPopupsState: 'add',
      optionalPagName: '',
      // table表格查询API配置对象
      dynamicApiConfig: {},
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode] &&
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode].tableComponents
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: this.name + '：',
              codeField: 'optionalPagName',
              isRequire: true,
              disabled: true,
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true,
              span: 8
            },
            {
              compKey: 'compKey2',
              labelName: '启用状态：',
              codeField: 'isEnableCode',
              component: () => import('@/components/org/isEnable/isEnableOne'),
              type: 'dropdownList',
              isMust: true,
              span: 8
            }
          ],
      // form 的表单对象
      formField: {
        isEnableCode: '1'
      },
      dynamicButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          clickEvent: () => this.car(),
          text: '保存',
          name: 'confirm', // 按钮图标样式
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey4',
          type: '',
          size: 'small',
          clickEvent: () => this.closeEdit(),
          text: '取消',
          name: 'cancel',
          position: 'right'
        }
      ]
    }
  },
  created() {},
  mounted() {
    // 数据回填
    if (this.editAdd === '编辑') {
      const data = JSON.parse(JSON.stringify(this.editData))
      data.isEnableCode === '停用' ? (data.isEnableCode = '0') : (data.isEnableCode = '1')
      if (this.titleName === '编辑车型与选装包关系') {
        this.formField.optionalPagName = data.optionalPagName
        this.formField.carOptionalId = data.carOptionalId // 选装包id
        this.formField.carTypeOptionalId = data.carTypeOptionalId // 选装包关系表id
      }
      if (this.titleName === '编辑车型与车身颜色关系') {
        this.formField.optionalPagName = data.carColorName
        this.formField.carColorId = data.carColorId // 车身色id
        this.formField.carTypeColorId = data.carTypeColorId // 选装包关系表id
      }
      if (this.titleName === '编辑车型与内饰颜色关系') {
        this.formField.optionalPagName = data.carIncolorName
        this.formField.carIncolorId = data.carIncolorId // 内饰色id
        this.formField.carTypeIncolorId = data.carTypeIncolorId // 选装包关系表id
      }
      this.formField.isEnableCode = data.isEnableCode
      this.formField.updateControlId = data.updateControlId // 并发控制id
    } else {
      this.formField.carConfigId = this.id
    }
  },
  methods: {
    // form表单组件每个元素所占栅格栏数每行24格
    spanNum() {
      return 24
    },
    // 保存
    car() {
      const returnObj = {}
      if (this.titleName === '编辑选装包') {
        returnObj.apiConfig = api.updateCarOptionalPag
      }
      if (this.titleName === '编辑车身颜色') {
        returnObj.apiConfig = api.updateCarColor
      }
      if (this.titleName === '编辑内饰颜色') {
        returnObj.apiConfig = api.updateCarInColor
      }
      if (this.titleName === '新增选装包') {
        returnObj.apiConfig = api.addCarOptionalPag
      }
      if (this.titleName === '新增车身颜色') {
        returnObj.apiConfig = api.addCarColor
      }
      if (this.titleName === '新增内饰颜色') {
        returnObj.apiConfig = api.addCarInColor
      }
      returnObj.variables = this.formField
      var paramD = this.$getParams(returnObj, null, this.dynamicApiStyle)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD, this.dynamicApiStyle).then(response => {
        if (response.result === '0') {
          this.$refs['multipleTable'].listLoading = false
          if (response.msg) {
            this.$message({
              message: response.msg,
              type: 'warn',
              duration: 2000
            })
          }
          this.closeEdit()
        }
        // 返回json
        this.$message({ message: response.msg })
        this.$refs['multipleTable'].listLoading = false
        this.closeEdit(this.name)
      }).catch(err => {
        console.log(err)
        this.$refs['multipleTable'].listLoading = false
      })
    },

    // 关闭弹窗
    closeEdit(name) {
      this.$emit('closeEdit', name)
    }
  }
}
</script>

